/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */
#include "../../stdafx.h"
#include "../../include/div/FCLayoutDiv.h"

namespace FaceCat{
	FCLayoutDiv::FCLayoutDiv(){
		m_autoWrap = false;
		m_layoutStyle = FCLayoutStyle_LeftToRight;
	}

	FCLayoutDiv::~FCLayoutDiv(){
	}

	bool FCLayoutDiv::autoWrap(){
		return m_autoWrap;
	}

	void FCLayoutDiv::setAutoWrap(bool value){
		m_autoWrap = value;
	}

	FCLayoutStyle FCLayoutDiv::getLayoutStyle(){
		return m_layoutStyle;
	}

	void FCLayoutDiv::setLayoutStyle(FCLayoutStyle value){
		m_layoutStyle = value;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////

	String FCLayoutDiv::getViewType(){
		return L"LayoutDiv";
	}

	void FCLayoutDiv::getAttribute(const String& name, String *value, String *type){
	    if (name == L"autowrap"){
            *type = L"bool";
			*value = FCTran::boolToStr(autoWrap());
        }
		else if (name == L"layoutstyle"){
			*type = L"enum:FCLayoutStyle";
			*value = FCTran::layoutStyleToStr(getLayoutStyle());
        }
		else{
			FCDiv::getAttribute(name, value, type);
		}
	}

	ArrayList<String> FCLayoutDiv::getAttributeNames(){
		ArrayList<String> attributeNames = FCDiv::getAttributeNames();
		attributeNames.add(L"AutoWrap");
		attributeNames.add(L"LayoutStyle");
		return attributeNames;
	}

	bool FCLayoutDiv::onResetLayout(){
		bool reset = false;
		if (getNative()){
			FCPadding padding = getPadding();
			int left = padding.left, top = padding.top;
			int width = getWidth() - padding.left - padding.right;
			int height = getHeight() - padding.top - padding.bottom;
			int viewSize = (int)m_views.size();
			int vPos = 0;
			for (int i = 0; i < viewSize; i++){
				FCView *view = m_views.get(i);
				if(view->isVisible() && view != getHScrollBar() && view != getVScrollBar()){
					FCSize size = view->getSize();
					int cLeft = view->getLeft(), cTop = view->getTop(), cWidth = size.cx, cHeight = size.cy;
					int nLeft = cLeft, nTop = cTop, nWidth = cWidth, nHeight = cHeight;
					FCPadding margin = view->getMargin();
					switch (m_layoutStyle){
                        //自下而上
                        case FCLayoutStyle_BottomToTop:{
                                if (vPos == 0){
                                    top = height - padding.top;
                                }
                                int lWidth = 0;
                                if (m_autoWrap){
                                    lWidth = size.cx;
                                    int lTop = top - margin.top - cHeight - margin.bottom;
                                    if (lTop < padding.top){
										if(vPos != 0){
											left += cWidth + margin.left;
										}
                                        top = height - padding.top;
                                    }
                                }
                                else{
                                    lWidth = width - margin.left - margin.right;
                                }
                                top -= cHeight + margin.bottom;
                                nLeft = left + margin.left;
                                nWidth = lWidth;
                                nTop = top;
                                break;
                            }
                        //从左向右
                        case FCLayoutStyle_LeftToRight:{
                                int lHeight = 0;
                                if (m_autoWrap){
                                    lHeight = size.cy;
                                    int lRight = left + margin.left + cWidth + margin.right;
                                    if (lRight > width){
                                        left = padding.left;
										if(vPos != 0){
											top += cHeight + margin.top;
										}
                                    }
                                }
                                else{
                                    lHeight = height - margin.top - margin.bottom;
                                }
                                left += margin.left;
                                nLeft = left;
                                nTop = top + margin.top;
                                nHeight = lHeight;
                                left += cWidth + margin.right;
                                break;
                            }
                        //从右向左
                        case FCLayoutStyle_RightToLeft:{
                                if (vPos == 0){
                                    left = width - padding.left;
                                }
                                int lHeight = 0;
                                if (m_autoWrap){
                                    lHeight = size.cy;
                                    int lLeft = left - margin.left - cWidth - margin.right;
                                    if (lLeft < padding.left){
                                        left = width - padding.left;
										if(vPos != 0){
											top += cHeight + margin.top;
										}
                                    }
                                }
                                else{
                                    lHeight = height - margin.top - margin.bottom;
                                }
                                left -= cWidth + margin.left;
                                nLeft = left;
                                nTop = top + margin.top;
                                nHeight = lHeight;
                                break;
                            }
                        //自上而下
                        case FCLayoutStyle_TopToBottom:{
                                int lWidth = 0;
                                if (m_autoWrap){
                                    lWidth = size.cx;
                                    int lBottom = top + margin.top + cHeight + margin.bottom;
                                    if (lBottom > height){
										if(vPos != 0){
											left += cWidth + margin.left + margin.right;
										}
                                        top = padding.top;
                                    }
                                }
                                else{
                                    lWidth = width - margin.left - margin.right;
                                }
                                top += margin.top;
                                nTop = top;
                                nLeft = left + margin.left;
                                nWidth = lWidth;
                                top += cHeight + margin.bottom;
                                break;
                            }
					}
					if (cLeft != nLeft || cTop != nTop || cWidth != nWidth || cHeight != nHeight){
						FCRect rect = {nLeft, nTop, nLeft + nWidth, nTop + nHeight};
                        view->setBounds(rect);
                        reset = true;
                    }
					vPos++;
				}
			}
		}
		return reset;
	}

	void FCLayoutDiv::setAttribute(const String& name, const String& value){
	    if (name == L"autowrap"){
			setAutoWrap(FCTran::strToBool(value));
        }
		else if (name == L"layoutstyle"){
			setLayoutStyle(FCTran::strToLayoutStyle(value));
        }
        else{
			FCDiv::setAttribute(name, value);
        }
	}

	void FCLayoutDiv::update(){
		if(m_native && !m_native->m_lockUpdate){
			onResetLayout();
			int viewsSize = (int)m_views.size();
			for (int i = 0; i < viewsSize; i++){
				m_views.get(i)->update();
			}
			updateScrollBar();
		}
	}
}